package cn.hs.lyxf.web.controller;

import cn.hs.lyxf.domain.ShopType;
import cn.hs.lyxf.query.ShopTypeQuery;
import cn.hs.lyxf.result.JsonResult;
import cn.hs.lyxf.result.PageList;
import cn.hs.lyxf.service.IShopTypeService;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/shopType")
public class ShopTypeController {

    @Autowired
    public IShopTypeService shopTypeService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody ShopType shopType) {
        if (shopType.getId() != null){
                shopTypeService.updateById(shopType);
        }else{
                shopTypeService.insert(shopType);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id) {
            shopTypeService.deleteById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id") Long id) {
        return JsonResult.success(shopTypeService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JsonResult list() {
        return JsonResult.success(shopTypeService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public JsonResult page(@RequestBody ShopTypeQuery query) {
        Page<ShopType> page = new Page<ShopType>(query.getPage(), query.getRows());
        page = shopTypeService.selectPage(page);
        return JsonResult.success(new PageList<ShopType>(page.getTotal(), page.getRecords()));
    }
    /**
     * 面包屑
     * @param shopTypeId
     * @return
     */
    @RequestMapping(value = "/crumbs/{shopTypeId}", method = RequestMethod.GET)
    public JsonResult getCrumbs(@PathVariable("shopTypeId") Long shopTypeId) {
        return JsonResult.success(shopTypeService.getCrumbs(shopTypeId));
    }
}
